package com.shan.domm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shan.domm.common.ErrorCode;
import com.shan.domm.exception.BusinessException;
import com.shan.domm.model.domain.Dorm;
import com.shan.domm.service.DormService;
import com.shan.domm.mapper.DormMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 23949
* @description 针对表【dorm】的数据库操作Service实现
* @createDate 2024-04-08 14:57:01
*/
@Service
public class dormServiceImpl extends ServiceImpl<DormMapper, Dorm>
    implements DormService {

    @Resource
    private DormMapper dormMapper;
    @Override
    public Boolean adddorm(Dorm dorm) {
        if(dorm==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        return this.save(dorm);
    }

    @Override
    public String deletedorm(Dorm dorm) {
        if(dorm==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Integer id = dorm.getId();
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Long l = dormMapper.selectCount(queryWrapper);
        if(l<1){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"请求数据不存在");
        }
        int delete = dormMapper.delete(queryWrapper);
        if(delete>0){
            return "删除成功";
        }
        return "删除失败";

    }

    @Override
    public String updatedorm(Dorm dorm) {
        if(dorm==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Integer id=dorm.getId();
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Long l = dormMapper.selectCount(queryWrapper);
        if(l<1){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"请求数据不存在");
        }
        int i = dormMapper.update(dorm, queryWrapper);
        if(i>0){
            return "更新成功";
        }
        return "更新失败";

    }

    @Override
    public Dorm searchdorm(String dormName) {
        if(dormName==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dorm_name",dormName);
        Long l = dormMapper.selectCount(queryWrapper);
        if(l<1){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"请求数据不存在");
        }
        Dorm dorm = dormMapper.selectOne(queryWrapper);
        return dorm;
    }
}




